package com.tucc.hello.leetcode.no1_400.no1_200.no81_100.no88;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums1 = {1, 2, 3, 0, 0, 0};
        int m = 3;
        int[] nums2 = {2, 5, 6};
        int n = 3;
        solution.merge(nums1, m, nums2, n);
        for (int i = 0; i < nums1.length; i++) {
            System.out.print(nums1[i] + " ");
        }
        System.out.println();
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int index = m + n - 1;
        while (index >= 0) {
            if (m == 0) {
                nums1[index] = nums2[--n];
            } else if (n == 0) {
                nums1[index] = nums1[--m];
            } else {
                if (nums1[m - 1] >= nums2[n - 1]) {
                    m--;
                    nums1[index] = nums1[m];
                } else {
                    n--;
                    nums1[index] = nums2[n];
                }
            }
            index--;
        }
    }

}
